﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pop3;
using System.Net.Mail;
using TPTecnicas.MessageProcessor.Common;
using TPTecnicas.MessageProcessor.Business;

namespace TPTecnicas.MessageConnection
{
    public class MailConnectionManagerException : System.ApplicationException
    {
        public MailConnectionManagerException(string str)
            : base(str)
        {

        }

    }
   

    public class POP3LibraryWrapper : IMessageConnection
    {
        private Pop3.Pop3MimeClient pop3;
		public bool IsConnected = false;

        public void Connect(string server, int socket,string username, string password)
        {
            try
            {
                pop3 = new Pop3.Pop3MimeClient(server, socket, true, username, password);

                pop3.Trace += new TraceHandler(Console.WriteLine);
                pop3.ReadTimeout = 60000; //give pop server 60 seconds to answer

                //establish connection
                pop3.Connect();

				if (pop3.Pop3ConnectionState == Pop3ConnectionStateEnum.Connected)
					IsConnected = true;
            }
            catch (MailConnectionManagerException mcme)
            {
                throw mcme;
            }
        }


		public void Connect(Account a)
		{
			Connect(a.Server, a.Socket, a.Mail, a.Password);
		}


        public void Disconnect()
        {
            //close connection
            pop3.Disconnect();
        }

        public void Stats(out int numberOfMails, out int sizeMailBox)
        {
            //get mailbox stats
            pop3.GetMailboxStats(out numberOfMails, out sizeMailBox);
        }


        public IMessage GetMessage(int id)
        {
            RxMailMessage mm;
            pop3.GetEmail(id, out mm);

            return (IMessage)mm;
        }


        public List<RxMailMessage> List(int numberOfMailsToList)
        {
            //get at most the xx first emails
            RxMailMessage mm;
            List<RxMailMessage> rdo = new List<RxMailMessage>();
            int downloadNumberOfEmails;
            int maxDownloadEmails = numberOfMailsToList;
            int numberOfMailsInMailbox, sizeMailBox = 0;

            Stats(out numberOfMailsInMailbox, out sizeMailBox);

            if (numberOfMailsInMailbox < maxDownloadEmails)
                downloadNumberOfEmails = numberOfMailsInMailbox;
            else
                downloadNumberOfEmails = maxDownloadEmails;

            for (int i = 1; i <= downloadNumberOfEmails; i++)
            {
                pop3.GetEmail(i, out mm);
                rdo.Add(mm);

                if (mm == null)
                    Console.WriteLine("Email " + i.ToString() + " cannot be displayed.");
                else
                    Console.WriteLine(mm.MailStructure());
            }

            return rdo;
        }


		public RxMailMessage GetLastMail()
		{
			//get at most the xx first emails
			RxMailMessage rdo;

			int numberOfMailsInMailbox, sizeMailBox = 0;

			Stats(out numberOfMailsInMailbox, out sizeMailBox);

			pop3.GetEmail(numberOfMailsInMailbox, out rdo);

			return rdo;			
		}
    }
}
